package com.service.lotterySystem.controller;

import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import com.service.lotterySystem.dto.AddActivityDto;
import com.service.lotterySystem.dto.QueryActivityInfoDto;
import com.service.lotterySystem.entity.OperateInfo;
import com.service.lotterySystem.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.service.config.constant.Constant.*;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/10 19:52
 * @description：
 */
@RestController
@RequestMapping("activityController")
@Api(value = "活动接口", tags = "活动接口")
public class ActivityController {
    @Resource
    private ActivityService activityService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增活动
     * @param
     * @return
     */
    @RequestMapping(value = "/addActivity", method = RequestMethod.POST)
    @ApiOperation(value = "新增活动")
    @ResponseBody
    public Result addActivity(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                              @RequestBody AddActivityDto activityDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                OperateInfo operateInfo = ModelMapperUtil.strictMap(redisUtil.getValue(cloudAuthToken + userId), OperateInfo.class);
                activityDto.setaReleaser(operateInfo.getOperateName());
                return activityService.addActivity(activityDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户未登录，请登录后重试");
    }

    /**
     * 根据类型查询活动 活动类型 0--历史活动（可查看中奖名单等信息）1--正在进行的活动 2--预热活动'
     * @param
     * @return
     */
    @GetMapping(value = "queryActivityByType",produces="application/json")
    @ApiOperation(value = "根据类型查询活动（不需要token）：0--历史活动（可查看中奖名单等信息）1--正在进行的活动 2--预热活动")
    @ResponseBody
    public Result getInfo(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                          @RequestParam("type") @ApiParam("type:0--历史活动（可查看中奖名单等信息）1--正在进行的活动 2--预热活动")Integer type) throws Exception {
        //if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
           // String userId = UserTokenManager.getUserId(cloudAuthToken);
           // if (redisUtil.exist(cloudAuthToken+userId)) {
                return activityService.queryByType(type);
          //  }
          //  return Result.failure(USERNAME_Off_SITE,usernameOffSite);
       // }
        //return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 根据活动id查询详细信息
     * @param
     * @return
     */
    @GetMapping(value = "queryActivityById",produces="application/json")
    @ApiOperation(value = "根据活动id查询详细信息")
    @ResponseBody
    public Result queryActivityById(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                          @RequestParam("aId") String activityId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) { // 密码修改
                QueryActivityInfoDto activityDto = activityService.queryById(activityId);
                return Result.ok().setData(activityDto);
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 修改活动信息
     * @param
     * @return
     */
    @PostMapping(value = "updateActivity",produces="application/json")
    @ApiOperation(value = "修改活动信息(可用于修改活动的状态,是否计算中奖人员中 1--计算中 0--还未开始 2--计算完毕)")
    @ResponseBody
    public Result updateActivity(@RequestHeader(value = "cloud-Auth-Token",required = false) String cloudAuthToken,
                                 @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card,
                                 @RequestBody QueryActivityInfoDto activityDto) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card != 1) {
                    return Result.failure("无权限操作");
                }
                activityService.updateById(activityDto);
                return Result.ok();
            }
            return Result.failure(USERNAME_Off_SITE,usernameOffSite);
        }
        return Result.failure(USERNAME_REPEAT,"用户名已存在，请重新输入");
    }

    /**
     * 删除活动信息
     * @param
     * @return
     */
    @GetMapping(value = "deleteActivity",produces="application/json")
    @ApiOperation(value = "删除活动信息")
    @ResponseBody
    public Result deleteQyCorpInfo(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card,
                                   @RequestParam @ApiParam("活动id") String activityId) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card != 1) {
                    return Result.failure("无权限操作");
                }
                return activityService.deleteById(activityId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 删除活动信息
     * @param
     * @return
     */
    @PostMapping(value = "uploadImage",headers = "Content-Type=multipart/form-data")
    @ApiOperation(value = "logo图片/海报图片上传")
    @ResponseBody
    public Result uploadImage(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                              @RequestParam("file")  @ApiParam("文件") MultipartFile file,
                              @RequestParam("aId")  @ApiParam("活动id")String aId,
                              @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card,
                              @RequestParam("isType")  @ApiParam("1--海报 2--背景图")Integer isType) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card != 1) {
                    return Result.failure("无权限操作");
                }
                return activityService.uploadImage(aId,file,isType);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 运营人员查询参加活动并中奖的人员的收货地址是否填写
     * 未填写则进行发送短信提醒该用户完善收货地址
     * @param
     * @return
     */
    @PostMapping(value = "queryUserAddress",produces="application/json")
    @ApiOperation(value = "运营人员查询参加活动人员的收货是否填写（舍弃）")
    @ResponseBody
    public Result queryUserAddress(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                              @RequestParam("aId")  @ApiParam("活动id")String aId,
                                   @RequestParam("uId")  @ApiParam("活动id")String uId,
                              @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card != 1) {
                    return Result.failure("无权限操作");
                }
                return activityService.queryUserAddress(aId,uId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
    /**
     * 运营人员发送未填写收货地址的用户发送短信
     * @param
     * @return
     */
    @PostMapping(value = "sendSms",produces="application/json")
    @ApiOperation(value = "运营人员发送未填写收货地址的用户发送短信（舍弃）")
    @ResponseBody
    public Result sendSms(@RequestHeader(value = "cloud-Auth-Token") String cloudAuthToken,
                                   @RequestParam("aId")  @ApiParam("活动id")String aId,
                                   @RequestParam("uId")  @ApiParam("活动id")String uId,
                                   @RequestParam(value = "card") @ApiParam("card:0--用户侧 1--运营侧")Integer card) throws Exception {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken+userId)) {
                if (card != 1) {
                    return Result.failure("无权限操作");
                }
                return activityService.sendSms(aId,uId);
            }else {
                return Result.failure(USERNAME_Off_SITE,usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN,"用户未登录");
    }
}
